package com.shaozj.system.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shaozj.core.enums.StatusEnum;
import com.shaozj.system.converter.DictTypeConverter;
import com.shaozj.system.mapper.SysDictTypeMapper;
import com.shaozj.system.model.entity.SysDictType;
import com.shaozj.system.model.form.DictTypeForm;
import com.shaozj.system.model.query.DictTypePageQuery;
import com.shaozj.system.model.vo.DictTypePageVO;
import com.shaozj.system.service.SysDictTypeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 字典类型业务实现类
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements SysDictTypeService {

    private final DictTypeConverter dictTypeConverter;

    @Override
    public Page<DictTypePageVO> getDictTypePage(DictTypePageQuery queryParams) {
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        String keywords = queryParams.getKeywords();

        Page<SysDictType> page = page(new Page(pageNum, pageSize), new LambdaQueryWrapper<SysDictType>()
                .and(StrUtil.isNotBlank(keywords),
                        wrapper -> wrapper.like(StrUtil.isNotBlank(keywords), SysDictType::getName, keywords)
                                .or()
                                .like(StrUtil.isNotBlank(keywords), SysDictType::getCode, keywords))
                .eq(SysDictType::getStatus, StatusEnum.ENABLE.getValue())
        );
        return dictTypeConverter.entity2PageVO(page);
    }

    @Override
    public DictTypeForm getDictTypeForm(Long id) {
        SysDictType dictType = getOne(new LambdaQueryWrapper<SysDictType>()
                .eq(SysDictType::getId, id)
                .select(
                        SysDictType::getId,
                        SysDictType::getName,
                        SysDictType::getCode,
                        SysDictType::getRemark,
                        SysDictType::getStatus
                )
        );
        return dictTypeConverter.entity2Form(dictType);
    }

    @Override
    public boolean saveDictType(DictTypeForm dictTypeForm) {
        log.info("=========dsfsdfsdfs======, {}", JSON.toJSONString(dictTypeForm));
        SysDictType dictType = dictTypeConverter.form2Entity(dictTypeForm);
        return save(dictType);
    }

    @Override
    public boolean updateDictType(Long id, DictTypeForm dictTypeForm) {
        SysDictType dictType = dictTypeConverter.form2Entity(dictTypeForm);
        return updateById(dictType);
    }

    @Override
    public boolean deleteDictType(String idStr) {
        Assert.isTrue(StrUtil.isNotBlank(idStr), "删除数据为空");
        List<Long> ids = Arrays.asList(idStr.split(","))
                .stream()
                .map(id -> Long.parseLong(id))
                .collect(Collectors.toList());
        return removeByIds(ids);
    }
}
